package com.autodatabase.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.autodatabase.model.FactoryItemInfo;

public class DatabaseConnection {
	public DatabaseConnection() {
		super();
	}
	
	public DatabaseConnection(FactoryItemInfo itemInfo) {
		super();
		this.itemInfo = itemInfo;
	}

	/**
	 * 测试连接是否正常
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public boolean testConnection() throws SQLException, ClassNotFoundException {
		return testConnection(itemInfo);
	}

	/**
	 * 测试连接是否正常
	 * @param itemInfo
	 * @return
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
	public boolean testConnection(FactoryItemInfo itemInfo) 
		throws SQLException, ClassNotFoundException 
	{
		Connection conn = getConnection(itemInfo);
		
		if(conn != null && !conn.isClosed()) {
			return true;
		}

		return false;
	}

	/**
	 * 获得连接
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public Connection getConnection() throws ClassNotFoundException, SQLException {
		return getConnection(itemInfo);
	}
	
	/**
	 * 获得连接
	 * @param itemInfo
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public Connection getConnection(FactoryItemInfo itemInfo)
		throws ClassNotFoundException, SQLException 
	{
		this.itemInfo = itemInfo;
		String driverClassName = getDriverClassName();
		String url = itemInfo.getUrl();
		String userName = itemInfo.getUserName();
		String password = itemInfo.getPassword();
		System.err.println("------------------------------------");
		System.err.println("driverClassName:" + driverClassName);
		System.err.println("url:" + driverClassName);
		System.err.println("userName:" + userName);
		System.err.println("password:" + password);
		System.err.println("------------------------------------");
		Connection conn = getConnection(driverClassName, url, userName, password);
		
		return conn;
	}

	/**
	 * 通过各个参数获得数据库连接
	 * @param driverClassName
	 * @param url
	 * @param userName
	 * @param password
	 * @return
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 */
	public Connection getConnection(String driverClassName, String url, 
		String userName, String password) throws ClassNotFoundException, SQLException 
	{
		Class.forName(driverClassName);
		Connection conn = DriverManager.getConnection(url, userName, password);
		return conn;
	}

	/**
	 * 取得driver class
	 * @return
	 */
	private String getDriverClassName() {
		if(itemInfo != null) {
			int type = itemInfo.getDbType();

			if(type == MYSQL) {
				return "com.mysql.jdbc.Driver";
			}
			else if(type == SQLSERVER) {
				return "";
			}
		}
		
		return "com.mysql.jdbc.Driver";
	}

	public FactoryItemInfo getItemInfo() {
		return itemInfo;
	}

	public void setItemInfo(FactoryItemInfo itemInfo) {
		this.itemInfo = itemInfo;
	}

	private FactoryItemInfo itemInfo;
	
	private static int MYSQL = 1;
	private static int SQLSERVER = 2;
}
